/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.TextRegler;
import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author Morten J, Morten B & Jonas
 */
public class Kasserapport extends javax.swing.JFrame {

    private boolean dialogOpen;
    private final Kasse kasse;

    /**
     * Creates new form Kasserapport
     *
     * @param kasse
     */
    public Kasserapport(view.Kasse kasse) {
	dialogOpen = false;
	this.kasse = kasse;
	initComponents();
	this.getContentPane().setBackground(new Color(186, 153, 108));
	this.setLocationRelativeTo(kasse);
	this.setAlwaysOnTop(true);
	this.setResizable(false);
	kasse.skrivebeskyttet(true);
	kasse.skrivebeskyttetBetal(true);
	GUI.getGuiHandler().toogleLogOut(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jOptaltKr = new javax.swing.JTextField();
        jOptaltEu = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jBtnFjern = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowIconified(java.awt.event.WindowEvent evt) {
                formWindowIconified(evt);
            }
        });

        jOptaltKr.setBackground(java.awt.Color.darkGray);
        jOptaltKr.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jOptaltKr.setForeground(new java.awt.Color(250, 250, 250));
        jOptaltKr.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(250, 250, 250)));
        jOptaltKr.setCaretColor(new java.awt.Color(250, 250, 250));
        jOptaltKr.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jOptaltKrKeyReleased(evt);
            }
        });

        jOptaltEu.setBackground(java.awt.Color.darkGray);
        jOptaltEu.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jOptaltEu.setForeground(new java.awt.Color(250, 250, 250));
        jOptaltEu.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(250, 250, 250)));
        jOptaltEu.setCaretColor(new java.awt.Color(250, 250, 250));
        jOptaltEu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOptaltEuActionPerformed(evt);
            }
        });
        jOptaltEu.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jOptaltEuKeyReleased(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(250, 250, 250));
        jLabel1.setText("kr.");

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(250, 250, 250));
        jLabel2.setText("€");

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(250, 250, 250));
        jLabel3.setText("Indtast optalt kassebeholdning:");

        jBtnFjern.setBackground(java.awt.Color.darkGray);
        jBtnFjern.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jBtnFjern.setForeground(new java.awt.Color(250, 250, 250));
        jBtnFjern.setText("OK");
        jBtnFjern.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(250, 250, 250)));
        jBtnFjern.setFocusPainted(false);
        jBtnFjern.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnFjernActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jOptaltKr, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addComponent(jOptaltEu))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jBtnFjern, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jOptaltKr, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jOptaltEu, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBtnFjern, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jOptaltEuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOptaltEuActionPerformed
	setKasse();
    }//GEN-LAST:event_jOptaltEuActionPerformed

    private void jBtnFjernActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnFjernActionPerformed
	setKasse();
    }//GEN-LAST:event_jBtnFjernActionPerformed

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
if (!dialogOpen) {
        this.requestFocus();
}
    }//GEN-LAST:event_formWindowLostFocus

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
	kasse.skrivebeskyttet(false);
	GUI.getGuiHandler().toogleLogOut(true);
    }//GEN-LAST:event_formWindowClosed

    private void formWindowIconified(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowIconified
	this.setState(NORMAL);
    }//GEN-LAST:event_formWindowIconified

    private void jOptaltKrKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jOptaltKrKeyReleased
	TextRegler.onlyDouble(jOptaltKr);
    }//GEN-LAST:event_jOptaltKrKeyReleased

    private void jOptaltEuKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jOptaltEuKeyReleased
	TextRegler.onlyDouble(jOptaltEu);
    }//GEN-LAST:event_jOptaltEuKeyReleased

    public void setKasse() {
	try {
	    double optaltKr = TextRegler.roundDouble(Double.parseDouble(jOptaltKr.getText()));
	    double optaltEu = TextRegler.roundDouble(Double.parseDouble(jOptaltEu.getText()));
	    if (!jOptaltEu.getText().isEmpty() || !jOptaltEu.getText().isEmpty()) {
		dialogOpen = true;
		int youSure = JOptionPane.showConfirmDialog(this, "Optalt kassebeholdning i kroner: "
			+ optaltKr + "\nOptalt kassebeholdning i euro: "
			+ optaltEu + "\n\nEr du sikker på, at du vil lukke kassen med de indtastede beholdninger?\n"
			+ "Kassen kan ikke benyttes efterfølgende!");
		if (youSure == 0) {
		    kasse.updateOptaltKasse(optaltKr, optaltEu);
		    kasse.closeKasse();
		    this.setVisible(false);
		    GUI.getGuiHandler().toogleLogOut(true);
		}
		dialogOpen = false;
	    }
	} catch (NumberFormatException ex) {

	}
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	/* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
	 */
	try {
	    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
		if ("Nimbus".equals(info.getName())) {
		    javax.swing.UIManager.setLookAndFeel(info.getClassName());
		    break;
		}
	    }
	} catch (ClassNotFoundException ex) {
	    java.util.logging.Logger.getLogger(Kasserapport.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (InstantiationException ex) {
	    java.util.logging.Logger.getLogger(Kasserapport.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (IllegalAccessException ex) {
	    java.util.logging.Logger.getLogger(Kasserapport.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (javax.swing.UnsupportedLookAndFeelException ex) {
	    java.util.logging.Logger.getLogger(Kasserapport.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
        //</editor-fold>

	/* Create and display the form */
	java.awt.EventQueue.invokeLater(new Runnable() {
	    public void run() {

	    }
	});
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnFjern;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jOptaltEu;
    private javax.swing.JTextField jOptaltKr;
    // End of variables declaration//GEN-END:variables
}
